// 职位权限模块
const express = require("express");
const router = express.Router();
const {
  getRolePermission,
  getPermission,
  addRole,
  editRole,
  deleteRole,
  editRolePermission,
} = require("../../router_handler/admin/role");

// 获取全部职位权限数据

router.get("/role/:page/:limit", async (req, res) => {
  const { page, limit } = req.params;
  const roleName = req.query.roleName;
  const offset = (page - 1) * limit;
  const result = await getRolePermission(roleName, offset, limit);
  res.send(result);
});

// 获取要分配的权限数据
router.get("/permission/toAssign/:id", async (req, res) => {
  const { id } = req.params;
  const result = await getPermission(id);
  res.send(result);
});
// 添加职位
router.post("/role/save", async (req, res) => {
  const { roleName } = req.body;
  const result = await addRole(roleName);
  res.send(result);
});
// 修改职位
router.put("/role/update", async (req, res) => {
  const { id, roleName } = req.body;
  const result = await editRole(id, roleName);
  res.send(result);
});
// 删除职位
router.delete("/role/remove/:id", async (req, res) => {
  const { id } = req.params;
  const result = await deleteRole(id);
  res.send(result);
});

// 修改职位权限
router.post("/permission/doAssign/", async (req, res) => {
  const { roleId, permissionId } = req.query;
  const result = await editRolePermission(roleId, permissionId);
  res.send(result);
});
module.exports = router;
